package com.lsmo.android.carfans.activity.car;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.app.TabActivity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.lsmo.android.carfans.R;
import com.lsmo.android.carfans.app.widget.RemoteImageView;
import com.lsmo.android.carfans.model.car.BrandTypeThread;
import com.lsmo.android.carfans.process.car.BrandTypeProcess;
import com.lsmo.android.carfans.view.car.SelectCarView;
import com.lsmo.android.common.base.BaseProcess;
import com.lsmo.android.common.base.BaseTask;
import com.lsmo.android.common.base.ViewProcessCallBack;

public class BrandTypeActivity extends TabActivity implements ViewProcessCallBack,View.OnClickListener {
	
	public static final String TAG = SelectCarView.class.getSimpleName();
	
	private BrandTypeActivity activity;
	private Button button[] = new Button[3];
	ContentResolver contentResolver;
	private boolean flag;
	private RemoteImageView image01;
	private TabHost mTabHost;
	private int suffix;
	private String title;
	private TextView view01;
	private Button fav_button;
	
	private BrandTypeProcess bTypeProcess;
	private String[] params = null;
	public static int ids,positions;
	public static String names;
	
	public Context mContext ;
	private ViewGroup mProgressBarLayout;
	private LinearLayout mLinearLayout;
	private LayoutInflater mInflater;
	private ArrayList<BrandTypeThread> BrandTypeList;
	
	public static void launch(Context c,int position,int id,String name){
		Intent intent = new Intent(c, BrandTypeActivity.class);
		ids = id;
		positions = position;
		names = name;
		c.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mInflater = this.getLayoutInflater();
		this.bTypeProcess = new BrandTypeProcess();
		this.mContext = BrandTypeActivity.this;
		
		initView();//加载view
		
		 this.suffix = ids;
		 changeButtonBg(0);//设置按钮状态
		    
		 addTab();//设置tabbar
		    
	     updateView();//加载页面数据
	}
	
	private void initView(){
		this.setContentView(R.layout.car_select_view_brandtype);
		mProgressBarLayout = (ViewGroup)this.findViewById(R.id.loading_layout);
		mLinearLayout = (LinearLayout)this.findViewById(R.id.car_select_brandType_tabView2);
 		TextView titleView = (TextView)this.findViewById(R.id.title_bar_layout_tv_id);
 		titleView.setGravity(Gravity.CENTER);
 		titleView.setText(names);//标题
 		Button btn02 = (Button)this.findViewById(R.id.car_select_brandType_button02);
 		Button btn03 = (Button)this.findViewById(R.id.car_select_brandType_button03);
 		Button btn06 = (Button)this.findViewById(R.id.car_select_brandType_button06);
 		btn02.setVisibility(View.GONE);
 		btn03.setVisibility(View.GONE);
 		btn06.setVisibility(View.GONE);
 	    
 	    button[0] = (Button)this.findViewById(R.id.car_select_brandType_button01);//车款
 	    button[0].setOnClickListener(this);
 	    
 	    button[1] = (Button)this.findViewById(R.id.car_select_brandType_button04);//评测
 	    button[1].setOnClickListener(this);
 		
 	    button[2] = (Button)this.findViewById(R.id.car_select_brandType_button05);//经销商
 	    button[2].setOnClickListener(this);
	    
	    view01 = (TextView)this.findViewById(R.id.car_select_brandType_text11);//经销商价格
	    image01 = (RemoteImageView)findViewById(R.id.car_select_brandType_image);//左侧图片
	    
	    fav_button = (Button)this.findViewById(R.id.car_select_brandType_button_save);
	    fav_button.setOnClickListener(this);
	    
	    //contentResolver= getContentResolver();
	}
	

	
	@Override
	public void processAfterTaskFinish(int code, Object result) {
		// TODO Auto-generated method stub
		if(code == BrandTypeTask.GET_BRANDTYPE_LIST){
			if(result!=null){
				setBrandTypeList();
			}
		}
		this.mProgressBarLayout.setVisibility(View.GONE);
	}

	@Override
	public void updateView() {
		// TODO Auto-generated method stub
		this.mProgressBarLayout.setVisibility(View.VISIBLE);
		   
			new BrandTypeTask(BrandTypeTask.GET_BRANDTYPE_LIST,
					this.mContext,
					this,
					this.bTypeProcess 
		 ).execute(new String[]{});
	}
	
	
	private class BrandTypeTask extends BaseTask{
 		public static final int GET_BRANDTYPE_LIST =6;
		private BrandTypeProcess bTypeProcess ;
		
		public BrandTypeTask(int code , Context context,ViewProcessCallBack view,BaseProcess process ) {
			super(code,context,view );
			this.bTypeProcess = (BrandTypeProcess) process;
		}
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}
		
		@Override
		protected Object doInBackground(String... params) {
			super.doInBackground(params);
			Object result = null;
	 
			switch(this.mCode){
			    
				case GET_BRANDTYPE_LIST:
					
					//加载数据
					result = bTypeProcess.getBrandTypeThreadList(params, ids);
					BrandTypeList =  (ArrayList<BrandTypeThread>) result;
					
					break;
			}
			return result;
		}
		
		@Override
		protected void onPostExecute(Object result) {
			super.onPostExecute(result);
			switch(this.mCode){
				 
				case GET_BRANDTYPE_LIST:
					
				 		mView.processAfterTaskFinish(this.mCode ,result );
				 	
				 	break;
		   }
		}
	}
	
	
	
	/**
	 * set车款列表
	 */
	public void setBrandTypeList(){
		if(BrandTypeList !=null){
			String csReferPirceString = BrandTypeList.get(0).getCsPriceRange();
			view01.setText(csReferPirceString);
		}
		if(BrandTypeList.get(0).getCsPic().length()>0){
			  //图标
	    	AssetManager localAssetManager = this.mContext.getResources().getAssets();
	    	String str1 = BrandTypeList.get(0).getCsPic();
	    	InputStream localInputStream;
			try {
				localInputStream = localAssetManager.open(str1);
				Drawable localDrawable = Drawable.createFromStream(localInputStream, str1);
		        this.image01.setBackgroundDrawable(localDrawable);
		        
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			Integer localInteger = Integer.valueOf(R.drawable.default_image);
	        this.image01.setDefaultImage(localInteger);
		}
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		//车款
		case R.id.car_select_brandType_button01 :
			this.mTabHost.setCurrentTab(0);
			changeButtonBg(0);
			break;
	    //评测
		case R.id.car_select_brandType_button04:
			this.mTabHost.setCurrentTab(1);
			changeButtonBg(1);
			break;
		//经销商
		case R.id.car_select_brandType_button05:
			this.mTabHost.setCurrentTab(2);
			changeButtonBg(2);
			break;
		//收藏
		case R.id.car_select_brandType_button_save:
			Toast.makeText(this, "该功能未开通！", 0).show();
			break;
		}
	}
	/**
	 * 改变按钮状态
	 * @param paramInt
	 */
	public void changeButtonBg(int paramInt){
		 switch (paramInt) {
		 	//车型
			case 0:
				this.button[0].setBackgroundResource(R.drawable.navigation_button_bg);
				this.button[1].setBackgroundResource(R.drawable.transparent);
				this.button[2].setBackgroundResource(R.drawable.transparent);
				break;
			//评测
			case 1:
				this.button[0].setBackgroundResource(R.drawable.transparent);
				this.button[1].setBackgroundResource(R.drawable.navigation_button_bg);
				this.button[2].setBackgroundResource(R.drawable.transparent);
				break;
			//经销商
			case 2:
				this.button[0].setBackgroundResource(R.drawable.transparent);
				this.button[1].setBackgroundResource(R.drawable.transparent);
				this.button[2].setBackgroundResource(R.drawable.navigation_button_bg);
				break;
		}
	 }
	
	public void addTab(){
		TabHost localTabHost = getTabHost();
		this.mTabHost = localTabHost;
		//车型
		TabHost.TabSpec localTabSpec1 = this.mTabHost.newTabSpec("CarSummaryActivity");
		Intent localIntent1 = new Intent(this, CarSummaryActivity.class);
	    int i = this.suffix;
	    localIntent1.putExtra("carid", i);
	    title=this.names;
	    localIntent1.putExtra("title", title);
	    localTabSpec1.setContent(localIntent1);
	    Drawable localDrawable1 = getResources().getDrawable(R.drawable.car_icon);
	    localTabSpec1.setIndicator("", localDrawable1);
	    this.mTabHost.addTab(localTabSpec1);
	    
	    //评测
	    TabHost.TabSpec localTabSpec2 = this.mTabHost.newTabSpec("EvaluatingActivity");
	    Intent localIntent2 = new Intent(this, CarEvaluatingActivity.class);
	    int m = this.suffix;
	    localIntent2.putExtra("carid", m);
	    title=this.names;
	    localIntent2.putExtra("title", title);
	    localTabSpec2.setContent(localIntent2);
	    Drawable localDrawable2 = getResources().getDrawable(R.drawable.car_icon);
	    localTabSpec2.setIndicator("", localDrawable2);
	    this.mTabHost.addTab(localTabSpec2);
	    
	    //经销商
	    TabHost.TabSpec localTabSpec3 = this.mTabHost.newTabSpec("DealerActivity");
	    Intent localIntent3 = new Intent(this, CarDealerActivity.class);
	    int n = this.suffix;
	    localIntent3.putExtra("carid", n);
	    title=this.names;
	    localIntent3.putExtra("title", title);
	    localTabSpec3.setContent(localIntent3);
	    Drawable localDrawable3 = getResources().getDrawable(R.drawable.car_icon);
	    localTabSpec3.setIndicator("", localDrawable3);
	    this.mTabHost.addTab(localTabSpec3);
	    
	    this.mTabHost.setCurrentTab(0);
	}
	
	  
}
